<!--
 * @Author: lemon 719771893@qq.com
 * @Date: 2025-03-01 13:00:34
 * @LastEditors: 李梦
 * @LastEditTime: 2025-03-08 06:04:39
 * @FilePath: /ranyu_anchor_new/src/views/register/components/AuthInfo.vue
 * @Description: 注册页面-AuthInfo组件
-->
<template>
  <div class="auth-container">
    <div class="title">上传材料</div>
    <div class="box">
      <div class="upload-box">
        <span class="upload-title">身份证正面照片</span>
        <SingleImageUpload
          v-model="fileObj.idCardFrontUrl"
          :data="{
            fileType: 1,
          }"
          accept=".jpg,.png,.jpeg"
        />
      </div>
      <div class="upload-box">
        <span class="upload-title">身份证反面照片</span>
        <SingleImageUpload
          v-model="fileObj.idCardReverseUrl"
          :data="{
            fileType: 1,
          }"
          accept=".jpg,.png,.jpeg"
        />
      </div>
      <div class="upload-box">
        <span class="upload-title">游戏段位照片</span>
        <SingleImageUpload
          v-model="fileObj.gameDanUrl"
          :data="{
            fileType: 1,
          }"
          accept=".jpg,.png,.jpeg"
        />
      </div>
    </div>
    <el-form label-width="150">
      <el-form-item label="手持身份证录制视频">
        <FileUpload
          v-model="fileObj.videoUrl"
          :data="{
            fileType: 4,
          }"
          :limit="1"
          :maxFileSize="500"
          accept=".mp4,.mov"
        />
      </el-form-item>
      <el-form-item label="合同扫描件">
        <FileUpload
          v-model="fileObj.pactScanUrl"
          :data="{
            fileType: 4,
          }"
          :limit="1"
          :maxFileSize="100"
          accept=".jpeg,.png,.pdf"
        />
      </el-form-item>
      <el-form-item label="音频文件">
        <FileUpload
          v-model="fileObj.recUrl"
          :data="{
            fileType: 4,
          }"
          :limit="1"
          :maxFileSize="100"
          accept=".mp3,.wav"
        />
      </el-form-item>
      <el-form-item label="视频文件">
        <FileUpload
          v-model="fileObj.masUrl"
          :data="{
            fileType: 4,
          }"
          :limit="1"
          :maxFileSize="500"
          accept=".mp4,.mov"
        />
      </el-form-item>
    </el-form>
    <!-- 示例 -->
    <div class="title">演示实例</div>
    <div class="box">
      <div class="box-item">
        <div class="text">手持身份证录制视频</div>
        <el-tooltip content="点击预览" placement="left">
          <img
            class="pic"
            :src="videoPic"
            @click="
              handlePreview(
                'https://static.ranyu.vip/2025/index/videos/%E6%89%8B%E6%8C%81%E8%BA%AB%E4%BB%BD%E8%AF%81%E8%AE%A4%E8%AF%81.mp4',
                'video'
              )
            "
          />
        </el-tooltip>
      </div>
      <div class="box-item">
        <div class="text">游戏段位示例图</div>
        <el-tooltip content="点击预览" placement="right">
          <img
            class="pic"
            src="https://static.ranyu.vip/2025/index/imges/anthor/%E6%B8%B8%E6%88%8F%E6%AE%B5%E4%BD%8D%E7%A4%BA%E6%84%8F%E5%9B%BE.jpg"
            @click="
              handlePreview(
                'https://static.ranyu.vip/2025/index/imges/anthor/%E6%B8%B8%E6%88%8F%E6%AE%B5%E4%BD%8D%E7%A4%BA%E6%84%8F%E5%9B%BE.jpg',
                'pic'
              )
            "
          />
        </el-tooltip>
      </div>
      <!-- 示例的预览弹窗 -->
      <el-dialog
        v-model="dialogVisible"
        title=""
        width="60%"
        style="max-height: 74%"
        @closed="handleClose"
      >
        <div v-if="previewType == 'pic'" class="video-box">
          <img style="width: 100%; margin-top: 20px" :src="dialogImageUrl" />
        </div>

        <div v-if="previewType == 'video'" class="video-box">
          <div class="desc">
            本人燃小豆，身份证号511111********4523，入驻燃欲平台成为主播，自愿遵守平台公示的协议和直播规范。
          </div>
          <video
            ref="videoPlayer"
            style="width: 100%; height: auto"
            controls
            preload="auto"
            :data-setup="{}"
            autoplay
          >
            <source :src="dialogVideoUrl" type="video/mp4" />
          </video>
        </div>
      </el-dialog>
    </div>
    <div class="title">合同模板</div>
    <div class="box" @click="download(contractUrl, {}, `合同模板(${fileName}).pdf`)">
      <div class="left">
        <img src="@/assets/register/file.png" alt="" class="icon" />
        <div class="download">
          <img
            src="@/assets/register/download.svg"
            alt=""
            style="width: 12px; height: 12px; margin-right: 2px"
          />
          <span>下载</span>
        </div>
      </div>
      <div class="right">
        请您仔细阅读合同内容并填写相关信息，签署完成后，将合同扫描并上传至上传材料中的合同扫描件处，
        并将原件寄于:{{ address }}
        <br />
        {{ receptor }}
      </div>
    </div>
    <div class="remind-box">
      <div v-show="!checked" class="remind">
        <span class="circle">!</span>
        <span style="color: #777575; font-size: 10px">请勾选当前协议</span>
      </div>
    </div>

    <div class="check">
      <el-checkbox
        v-model="checked"
        style="display: flex; align-items: flex-start; padding-top: 3px"
        label=""
        size="large"
        @change="handleSelect"
      />
      <span style="font-size: 14px">
        请勾选并同意
        <span style="color: #66b1ff; cursor: default" @click="openProtocol">
          《RANYU主播入驻与直播协议》
        </span>
        ,
        <span style="color: #66b1ff; cursor: default" @click="openProtocol">
          《RANYU直播行为规范》
        </span>
        的全部内容，同意按照以上内容来配合执行，如有违规行为，主播本人愿意配合并按照以上内容的处罚决议执行。
      </span>
    </div>
    <div class="btn">
      <el-button type="info" @click="backStep">上一步</el-button>
      <el-button type="primary" :loading="isLoading" @click="handleSubmit">
        提交材料并申请
      </el-button>
    </div>
    <!-- 协议弹窗 -->
    <el-dialog
      v-model="protocolVisible"
      style="
        width: 50%;
        height: 60vh;
        display: flex;
        flex-direction: column;
        justify-content: space-between;
      "
    >
      <div class="docx-style" @scroll="handleScroll">
        <vue-office-docx
          src="https://static.ranyu.vip/2025/index/pdf/anchor/%E3%80%8ARANYU%E4%B8%BB%E6%92%AD%E5%85%A5%E9%A9%BB%E4%B8%8E%E7%9B%B4%E6%92%AD%E5%8D%8F%E8%AE%AE%E3%80%8B.docx"
        />
        <vue-office-docx
          src="https://static.ranyu.vip/2025/index/pdf/anchor/%E3%80%8ARANYU%E7%9B%B4%E6%92%AD%E8%A1%8C%E4%B8%BA%E8%A7%84%E8%8C%83%E3%80%8B.docx"
        />
      </div>
      <div class="btn-box">
        <el-button type="primary" :disabled="isRead" @click="handleAccepted">接受并同意</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script lang="ts">
export default {
  name: "AuthInfo",
};
</script>
<script setup lang="ts">
import VueOfficeDocx from "@vue-office/docx";
import SingleImageUpload from "@/components/Upload/SingleImageUpload.vue";
import FileUpload from "@/components/Upload/FileUpload.vue";
import { ref, inject } from "vue";
import videoPic from "@/assets/register/video.svg";
import { RegisterAuthInfo } from "@/api/auth/index";
import { download } from "@/utils/index";

const { contractUrl, fileName } = inject("contractUrl");
const isLoading = inject("isLoading");
const props = defineProps({
  info: {
    type: Object,
    default: () => ({}),
  },
});
const fileObj = ref<RegisterAuthInfo>({
  ...props.info,
  videoUrl: props.info.videoUrl ? [props.info.videoUrl] : [],
  pactScanUrl: props.info.pactScanUrl ? [props.info.pactScanUrl] : [],
  recUrl: props.info.recUrl ? [props.info.recUrl] : [],
  masUrl: props.info.masUrl ? [props.info.masUrl] : [],
});
const rename = (filename: string, url: string) => {
  let a = document.createElement("a");
  a.style = "display: none"; // 创建一个隐藏的a标签
  a.download = filename;
  a.href = url;
  document.body.appendChild(a);
  a.click(); // 触发a标签的click事件
  document.body.removeChild(a);
};
watch(
  () => props.info, // 监听 props.info
  (newVal) => {
    fileObj.value = {
      ...newVal,
      videoUrl: newVal.videoUrl ? [{ url: newVal.videoUrl }] : [],
      pactScanUrl: newVal.pactScanUrl ? [{ url: newVal.pactScanUrl }] : [],
      recUrl: newVal.recUrl ? [{ url: newVal.recUrl }] : [],
      masUrl: newVal.masUrl ? [{ url: newVal.masUrl }] : [],
    }; // 更新 baseInfo
    console.log("authInfo:", newVal);
  },
  { immediate: true, deep: true } // 立即执行一次
);

const address = "成都市青羊区光华北三路与日月大道西城新中心盈信•西城大厦D座1101。"; // 寄件地址
const receptor = "收件人：燃欲 电话：028-8885 0808 手机：13551350241";
const emit = defineEmits(["back", "submit"]);
// 上一步
const backStep = () => {
  emit("back");
};

const checked = ref(false); // 是否同意协议

// 监听滚动
const handleScroll = (e: any) => {
  const div = e.target;
  const { scrollTop, clientHeight, scrollHeight } = div;

  // 判断是否滚动到底部
  if (scrollTop + clientHeight >= scrollHeight - 10) {
    isRead.value = false;
  }
};

const handleAccepted = () => {
  console.log("接受并同意");
  checked.value = true;
  protocolVisible.value = false;
};

const handleSelect = (value: Boolean) => {
  if (!value) return;
  checked.value = false;
  protocolVisible.value = true;
};

const protocolVisible = ref(false); // 协议弹窗显示/隐藏
const openProtocol = () => {
  protocolVisible.value = true;
};

const dialogVisible = ref(false);
const previewType = ref("pic");
const dialogImageUrl = ref(""); // 示例：预览 图片地址
const dialogVideoUrl = ref(""); // 示例：视频地址
const handlePreview = (url: string, type: string) => {
  previewType.value = type;
  type == "pic" ? (dialogImageUrl.value = url) : (dialogVideoUrl.value = url);
  dialogVisible.value = true;
};
// 关闭预览弹窗
const handleClose = () => {
  dialogVisible.value = false;
  dialogImageUrl.value = "";
  dialogVideoUrl.value = "";
  previewType.value = "";
};

const isRead = ref(true); // 是否阅读并同意协议

// 提交
const handleSubmit = () => {
  if (!fileObj.value.idCardFrontUrl) {
    ElMessage({
      message: "请上传身份证正面照片",
      type: "warning",
    });
  } else if (!fileObj.value.idCardReverseUrl) {
    ElMessage({
      message: "请上传身份证反面照片",
      type: "warning",
    });
  } else if (fileObj.value.videoUrl.length === 0) {
    ElMessage({
      message: "请上传手持身份证录制视频",
      type: "warning",
    });
  } else if (fileObj.value.pactScanUrl.length === 0) {
    ElMessage({
      message: "请上传合同扫描件",
      type: "warning",
    });
  } else if (fileObj.value.recUrl.length === 0) {
    ElMessage({
      message: "请上传音频文件",
      type: "warning",
    });
  } else if (fileObj.value.masUrl.length === 0) {
    ElMessage({
      message: "请上传视频文件",
      type: "warning",
    });
  } else if (!checked.value) {
    ElMessage({
      message: "请勾选并同意相关协议",
      type: "warning",
    });
  } else {
    const params: RegisterAuthInfo = {
      idCardFrontUrl: fileObj.value.idCardFrontUrl, // 身份证正面照片
      idCardReverseUrl: fileObj.value.idCardReverseUrl, // 身份证反面照片
      gameDanUrl: fileObj.value.gameDanUrl, // 游戏段位照片
      videoUrl: fileObj.value.videoUrl[0].url, // 手持身份证录制视频
      pactScanUrl: fileObj.value.pactScanUrl[0].url, // 合同扫描件
      recUrl: fileObj.value.recUrl[0].url, // 音频文件
      masUrl: fileObj.value.masUrl[0].url, // 视频文件
    };
    emit("submit", params);
  }
};
</script>

<style lang="scss" scoped>
.auth-container {
  margin: 0px auto;
  width: 40%;

  .title {
    font-size: 14px;
    color: #3d3d3d;
    font-weight: bold;
    padding: 20px 0;
  }
  .box {
    display: flex;
    gap: 10px 20px;
    padding: 10px 10px 50px;
    cursor: default;
    .upload-box {
      display: flex;
      flex-direction: column;
      align-items: center;
      .upload-title {
        margin-bottom: 10px;
        font-size: 14px;
        color: #4e5969;
      }
    }
    &-item {
      // width: 150px;
      // height: 150px;
      .text {
        text-align: center;
        padding: 8px;
        font-size: 14px;
        color: #4e5969;
      }
      .pic {
        height: 150px;
        // width: 150px;
        color: #4e5969;
        border-radius: 8px;
        border: 1px dashed #3d3d3d;
      }
    }
    .left {
      width: 76px;
      height: 76px;
      background: #ffffff;
      border-radius: 8px;
      padding: 6px;
      box-sizing: border-box;
      display: flex;
      align-items: center;
      flex-direction: column;
      .icon {
        width: 32px;
        height: 32px;
      }
      .download {
        width: 42px;
        height: 17px;
        border-radius: 2px;
        background: #165dff;
        margin-top: 10px;
        color: #ffffff;
        font-size: 10px;
        padding: 0 4px;
        display: flex;
        align-items: center;
      }
    }
    .right {
      width: 800px;
      padding: 10px 20px;
      color: #3d3d3d;
    }
  }
  .check {
    margin-top: 10px;
    display: flex;
    align-items: flex-start;
  }
  .remind-box {
    height: 30px;
    .remind {
      width: 169px;
      padding: 4px;
      border-radius: 2px;
      background: #fdf5e8;

      box-sizing: border-box;
      border: 0.5px solid #f2c975;
      display: flex;
      align-items: center;
      .circle {
        display: inline-block;
        width: 22px;
        height: 22px;
        background: orange;
        margin-right: 10px;
        margin-left: 10px;
        border-radius: 50%;
        color: #ffffff;
        text-align: center;
      }
    }
  }

  .btn {
    margin-top: 24px;
    text-align: right;
  }
  .video-box {
    margin: auto;
    width: 100%;
    overflow: hidden;

    .desc {
      width: 100%;
      text-align: center;
      padding: 10px;
      font-weight: bold;
      font-size: 16px;
    }
  }
}
::v-deep .docx-wrapper {
  padding: 0 !important;
  background: none !important;
}
::v-deep .docx-wrapper > section.docx {
  margin-bottom: 0;
  padding: 20px 50px !important;
}
.docx-style {
  width: 100%;
  height: 48vh;
  overflow-y: scroll;
  scrollbar-width: none; /* Firefox */
  -ms-overflow-style: none; /* Internet Explorer 10+ */
  background-color: #fff;
}
/* 对于 WebKit 浏览器（如 Chrome, Safari） */
.docx-style::-webkit-scrollbar {
  display: none; /* Chrome, Safari, Opera*/
}
.btn-box {
  width: 100%;
  padding: 12px;
  text-align: center;
}
</style>
